package cc.dd.marketing.reward.parser;

import cc.dd.marketing.api.constant.FunctionCodeEnum;
import cc.dd.marketing.api.response.base.BaseResponse;
import cc.dd.marketing.core.AbstractRequestDTOParser;
import cc.dd.marketing.core.annotation.FunctionMapper;
import cc.dd.marketing.core.annotation.RewardMapper;
import cc.dd.marketing.core.base.ResponseParser;
import cc.dd.marketing.core.constant.CouponTemplateEnum;
import cc.dd.marketing.entity.ActivityReward;
import cc.dd.marketing.reward.dto.RewardGivingDTO;
import cc.dd.marketing.reward.dto.RewardGivingRequestDTO;
import cc.dd.marketing.reward.dto.RewardGivingRespDTO;
import cc.dd.marketing.reward.rule.domain.StockRemainRule;
import cc.dd.marketing.common.ContextParam;
import cc.dd.marketing.rule.base.Rule;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 组装
 *
 * @author dongsufeng
 * @date 2022/9/9 2:29 PM
 */
@Component
@Slf4j
@FunctionMapper(FunctionCodeEnum.PARTICIPATE)
@RewardMapper(CouponTemplateEnum.DEFAULT)
public class RewardParser extends AbstractRequestDTOParser<RewardGivingRequestDTO, RewardGivingDTO> implements ResponseParser<RewardGivingRequestDTO, RewardGivingDTO> {

    @Override
    protected RewardGivingDTO assembleDTO(RewardGivingRequestDTO request, RewardGivingDTO baseRequestDTO) {
        log.info("reward do assembleDTO");
        if (Objects.isNull(baseRequestDTO)) {
            baseRequestDTO = new RewardGivingDTO();
        }
        ActivityReward activityReward = new ActivityReward();
        //自定义规则，查库
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("transAmount","5");
        activityReward.setRule(jsonObject.toJSONString());
        activityReward.setStockRemain(100);
        baseRequestDTO.setActivityReward(activityReward);
        return baseRequestDTO;
    }

    @Override
    protected List<Rule> buildRules(RewardGivingDTO rewardGivingDTO) {
        log.info("reward do buildRules");
        List<Rule> rules = new ArrayList<>();
        rules.add(new StockRemainRule(rewardGivingDTO.getActivityReward().getStockRemain()));
        return rules;
    }

    @Override
    public BaseResponse buildResponse(ContextParam<RewardGivingRequestDTO, RewardGivingDTO> contextParam) {
        log.info("reward do buildResponse");
        RewardGivingRespDTO rewardGivingRespDTO = new RewardGivingRespDTO();
        rewardGivingRespDTO.setCouponCode("1111111");
        return rewardGivingRespDTO;
    }

}
